package com.base.cn.platform.os.controller.course.cou.record;

import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.controller.cmmon.ApiBaseController;
import com.base.cn.platform.os.service.course.cou.record.CourseLearnFlowService;
import com.base.cn.platform.os.service.course.cou.record.CoursePlayRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 课程播放记录接口Controller
 *
 * @author s.li
 * @create 2018-09-07-16:00
 */
@RestController
@RequestMapping("/api/play/record")
public class ApiCoursePlayRecordController extends ApiBaseController {

    @Autowired
    private CoursePlayRecordService coursePlayRecordService;
    @Autowired
    private CourseLearnFlowService courseLearnFlowService;


    /**
     * 保存课程播放记录
     * @param userId 用户ID
     * @param buyCourseId 套餐/课程ID（购买的课程ID）
     * @param courseId 课程ID
     * @param catalogId 目录ID
     * @param source 播放来源 1.WEB 2.H5 3.APP 4.小程序
     * @param finish 是否完成观看 1.未完成 2.已完成
     * @param duration 播放时长（单位/秒）
     * @return
     */
    @RequestMapping("/savePlayRecord")
    @ResponseBody
    public Map<String, Object> savePlayRecord(@RequestParam("userId") BigDecimal userId,
                                              @RequestParam("buyCourseId") BigDecimal buyCourseId,
                                              @RequestParam("courseId") BigDecimal courseId,
                                              @RequestParam("catalogId") BigDecimal catalogId,
                                              @RequestParam("source") int source,
                                              @RequestParam("finish") int finish,
                                              @RequestParam("duration") int duration) {
        Map<String, Object> playRecord = new HashMap<>();
        playRecord.put("userId", userId);
        playRecord.put("buyCourseId", buyCourseId);
        playRecord.put("courseId", courseId);
        playRecord.put("catalogId", catalogId);
        playRecord.put("source", source);
        playRecord.put("finish", finish);
        playRecord.put("duration", duration);
        return coursePlayRecordService.saveCoursePlayRecord(playRecord);
    }

    /**
     * 获取用户播放记录
     * @param userId 用户ID
     * @param currentPage 页码
     * @return Map<String,Object>
     */
    @RequestMapping("/findUserPlayRecordList")
    public Map<String,Object> findUserPlayRecordList(@RequestParam("userId") BigDecimal userId,
                                                     @RequestParam("currentPage") int currentPage){
        Map<String,Object> params = new HashMap<>();
        params.put("userId",userId);
        Map<String,Object> dataMap = courseLearnFlowService.findCoursePlayRecordFlowPage(params,currentPage,10,true,false,true,true);
        return ResultUtil.SUCCESS(dataMap);
    }

    /**
     * 查询用户最后一个播放记录
     * @param userId 用户ID
     * @return Map<String,Object>
     */
    @RequestMapping("/getUserLastPlayCourseRecord")
    public Map<String,Object> getUserLastPlayCourseRecord(@RequestParam("userId") BigDecimal userId){
        Map<String,Object> coursePlayRecord = coursePlayRecordService.getUserLastPlayCourseRecord(userId);
        return ResultUtil.SUCCESS(coursePlayRecord);
    }
}
